Intermediate Python¶
Table of Contents¶
- Syntax Examples
- Lists
- Dictionaries
- Built-in Functions (input())
- File I/O
- Classes
- Data Processing
- Graphing
Syntax Examples:¶
# This is a comment
#defining functions
def do_function(numArg):
newNum = numArg + 5
return newNum
#classes
class Person:
# this part is not needed
name = "Adam"
age = 21
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return "Hello, I'm " + name + "and I am " + str(age)
#if statements:
if do_function(5) <= 9:
do_function(10)
elif do_function(5) > 10:
print("HUGE")
else:
print("lorem ipsum")
#for loop
for i in range(1,10):
print("blah")
do_function(i)
#for each loop
word = "sha-boing"
for letter in word:
print(letter)
flag = True
#while loop
while flag == True:
print("another loop")
do_function(5)
flag = False
lorem ipsum blah blah blah blah blah blah blah blah blah s h a - b o i n g another loop
Non-primitive Objects¶
List¶
- a list is a named set of ordered objects enclosed in brackets
- ints, doubles, strings etc.
- the list can contain all of the same object types or different types
- examples: [0, 5, 1, 5, -30, 200] ["a", 'b', 123]
- How would you access the second element of the following list?
- use NAME [ INDEX ] where the index starts at 0
a = [12, 23, 45]
a[1]
23
Iterating through a list¶
Stepping through a list is a common occurance in programming and you will probably have to do it for your hackathon project. Lets look at some strategies below.
a = [1234, 234, 452 ,5674, 465, 67, 774, 74, 43, 463, 54]
for i in range(0,11):
print(a[i])
1234 234 452 5674 465 67 774 74 43 463 54
The range function sets a range of values for the loop to iterate through. As you can see from the printed output, the first number is included in the range, and the second number is not. There is also a third argument you can pass into the range function, which dictates the increment taken. Lets try an increment of two.
for i in range(0,11,2):
print(a[i])
1234 452 465 774 43 54
You could also use a for each loop so that the range just adjusts to the size of your list. Example below.
for number in a:
print(number)
print("\nPrinting second list\n")
b = [1,2,3]
for digit in b:
print(digit)
1234 234 452 5674 465 67 774 74 43 463 54 Printing second list 1 2 3
List Member Functions:¶
NumPy Arrays¶
NumPy arrays act much like lists, but they are fasters and closer to true arrays. Here is how to instantiate them.
import numpy as np
#creates a one dimensional array
arr = np.array([1,2,3,4,5])
#creates a two dimentional array
arr_2d = np.array([[1,2,3], [4,5,6], [7,8,9]])
Here is how to access the elements of the array
print(arr[0])
print(arr_2d[1,1])
1 5
For more information on NumPy arrays, visit: https://www.w3schools.com/python/numpy/default.asp
Dictionary¶
- an unordered set of key value pairs enclosed in curly brackets
- key: must be unique to the dictionary, used to access the value
- the key and value can be different types, but the key should only be an int, float, or string
- example: {0: "a", 1:"b", 2:"c"} {"the": [0, 1, 2], "cat": "Dog"}
How would you access the value "a" in the following dictionary?
# create dictionary
d = {2: "a", 3:"b", 7:"c", 12:"apple"}
# add an element
d[4] = "sun"
# two ways to get the value from the key
print(d.get(2))
print(d[2])
a a
# if you want only the keys
print(d.keys())
dict_keys([2, 3, 7, 12, 4])
# to access all the items
print(d.items())
dict_items([(2, 'a'), (3, 'b'), (7, 'c'), (12, 'apple'), (4, 'sun')])
How would you access the third key-value pair?
Dictionary Member Functions:¶
Exercise: From the dictionary print the third value and second key-value pair.¶
# print the third value
# print the second key-value pair
Built-In Functions:¶
Input¶
- The functon input() can be used to take in input from the user, and can be stored in a variable to be used in your program.
# This is a guess the number game.
import random
guessesTaken = 0
print('Hello! What is your name?')
myName = input()
number = random.randint(1, 20)
print('Well, ' + myName + ', I am thinking of a number between 1 and 20.')
while guessesTaken < 6:
print('Take a guess.')
guess = input()
guess = int(guess)
guessesTaken = guessesTaken + 1
if guess < number:
print('Your guess is too low.')
elif guess > number:
print('Your guess is too high.')
else:
break
if guess == number:
guessesTaken = str(guessesTaken)
print('Good job, ' + myName + '! You guessed my number in ' + guessesTaken + ' guesses!')
if guess != number:
number = str(number)
print('Nope. The number I was thinking of was ' + number)
Hello! What is your name? Sawyer Well, Sawyer, I am thinking of a number between 1 and 20. Take a guess. 5 Your guess is too low. Take a guess. 10 Your guess is too high. Take a guess. 6 Your guess is too low. Take a guess. 7 Good job, Sawyer! You guessed my number in 4 guesses!
More info:¶
For info on the rest of the built-in functions, you can see the python official documentation here: https://docs.python.org/3/library/functions.html
Or you can look at a less official and less boring site here: https://www.w3schools.com/python/python_ref_functions.asp
File I/O¶
Modes to Open files:
'r': Open a file for reading. (default)
'w': Open a file for writing. Creates a new file if it does not exist
Lets write to a file:
# open the file in write mode
file = open('text.txt', 'w')
# write to the file
file.write("My name is \n")
file.write("My favorite color is\n")
file.write("If i could have any pet it would be a ")
file.write("Would you rather live in the ocean or on the moon?")
file.write("I would rather...")
# YOUR TURN! Write something to the file
17
# always close our files
file.close()
Now lets read from the file:
infile = open('text.txt', 'r')
print(infile.read())
infile.close()
My name is My favorite color is If i could have any pet it would be a Would you rather live in the ocean or on the moon?I would rather...
Exercise: Create your own file, write to it, and read from it!¶
File Member Functions:¶
Classes¶
Think of a class as something like a dog. Dogs have plenty of info about them like name, breed, height, weight, age, and color. In terms of a class, those would be properties. This is the data part of the object. When you are making a class, make sure you use properties that every instance should have. Let's start making our dog class below.
class MyDog:
name = "Spot"
breed = "Lab"
height = 25
weight = 50
age = 5
color = "black"
You can access properties by typing the instance, then a '.' and then the property name.
Like I said earlier, not all dogs are labs or 50 pounds, so lets make an "init" function so people can set the values of their dog instances.
class initDog:
def __init__(self, name, breed, height, weight, age, color):
self.name = name
self.breed = breed
self.height = height
self.weight = weight
self.age = age
self.color = color
Now that we can initialize any dog we want, lets make one. I'll base it off of my own.
myDog = initDog("Riley", "pointer-terrier", 26, 50, 7, "brown")
print("My dog's name is " + myDog.name)
My dog's name is Riley
You can also define a "str" function in your classes so that they print how you want to. Lets try it below.
class initDog:
def __init__(self, name, breed, height, weight, age, color):
self.name = name
self.breed = breed
self.height = height
self.weight = weight
self.age = age
self.color = color
def __str__(self):
return "My name is " + self.name + " and I am a " + self.breed
myDog = initDog("Riley", "pointer-terrier", 26, 50, 7, "brown")
print(myDog)
My name is Riley and I am a pointer-terrier
We have all the info saved for dogs, but we dont have their favorite activities! We need to make some functions that make the dog do everything it loves doing. This is the behavior part of an object.
My dog Riley loves to nap and play tug-o-war. Lets define those methods below to complete our dog class!
class Dog:
def __init__(self, name, breed, height, weight, age, color):
self.name = name
self.breed = breed
self.height = height
self.weight = weight
self.age = age
self.color = color
def __str__(self):
return "My name is " + self.name + " and I am a " + self.breed
def tug_o_war(self, human_strength):
if human_strength < 10:
print(self.name + " won! You gotta pull harder than that!")
else:
print("You won. Nice job!")
def nap(self, duration):
print("I'm off to go take a nap. See you in " + str(duration) + " minutes")
myDog = Dog("Riley", "pointer-terrier", 26, 50, 7, "brown")
print(myDog)
print("...")
print("Time to play tug-o-war")
print("...")
myDog.tug_o_war(5)
print("...")
myDog.nap(45)
My name is Riley and I am a pointer-terrier ... Time to play tug-o-war ... Riley won! You gotta pull harder than that! ... I'm off to go take a nap. See you in 45 minutes
Classes and objects are two of the few key building blocks of Object Oriented Programming. They are used to model behavior and encapsulate data. Using classes in your code will make it cleaner, easier to read, and less repeptitive. If you are dealing with data that is tied together, copying and pasting code, or doing something over and over again, consider making a class.
Exercise¶
Think of something else you could make a class for, and write a quick class and a couple funcitons for it
# write the class here:
Data Processing Basics¶
Data science is the study of data to extract meaningful insights. It is a multidisciplinary approach that combines principles and practices from the fields of mathematics, statistics, artificial intelligence, and computer engineering to analyze large amounts of data. Data science is what allows our AI models to work on the Kria board and also allows us to analyze the output of the models
Today's lesson will have a pretty basic example of data processing and a more advanced one that involves file I/O. You don't have to understand it all, the main objective it to give you some examples of basic data processing and give you an idea of what it entails.
Basketball statistics Example¶
# Points per game stats of a theoretical basketball team
average_ppg = [13.1, 1.7, 1.8, 7.8, 1.2, 12.9, 10.6, 5.8, 3.0, 9.7, 18.6, 0.5, 7.6, 0.1]
average_ppg.sort(reverse = True)
print()
print("Average points per game list is displayed below in descending order.\n")
print(average_ppg)
print()
#General List Statistics
print("There are " + str(len(average_ppg)) + " players on the team")
# Average (mean) of the numbers
sum_of_ppg_list = sum(average_ppg)
average_ppg_of_list = round(sum_of_ppg_list / len(average_ppg),2)
print("The mean ppg is: " + str(average_ppg_of_list))
# Median number
number_of_elements_in_list = len(average_ppg)
if (number_of_elements_in_list % 2) == 0:
locate_median = number_of_elements_in_list // 2
ppg_median = round(((average_ppg[locate_median] + average_ppg[locate_median - 1]) / 2),2)
else:
locate_median = round((0.5 * (number_of_elements_in_list + 1)),2)
print("The median ppg is: " + str(ppg_median))
# Smallest number
print("The minimum ppg is: " + str(min(average_ppg)))
# Largest number
print("The maximum ppg is: " + str(max(average_ppg)) + "\n")
Average points per game list is displayed below in descending order. [18.6, 13.1, 12.9, 10.6, 9.7, 7.8, 7.6, 5.8, 3.0, 1.8, 1.7, 1.2, 0.5, 0.1] There are 14 players on the team The mean ppg is: 6.74 The median ppg is: 6.7 The minimum ppg is: 0.1 The maximum ppg is: 18.6
As you can see, this code takes a list of points per game of each player on a basketball team and calculates some simple statistics on it.
Fitbit Data Analysis Example¶
This block is just a bunch a functions that we will be using in a bit. Run this code before you run the next code to make sure that all the proper functions are defined. It shouldn't do anything, but it's necessary for the next example to work properly.
#reads data in from the file
def read_data(filename):
infile = open(filename, "r") # "r" for reading
data = infile.readlines()
infile.close()
return data
#removes new line character from data
def clean_lines(lines_param):
for i in range(len(lines_param)):
lines_param[i] = lines_param[i].strip()
#organizes the data into a table separated by commas
def restructure_data_into_table(data):
table = []
for data in data:
values = data.split(",")
table.append(values)
return table
#displays the data in a readable format to the user
def display_data(headers, table):
for i in range(len(headers)):
print(headers[i], end="\t\t")
print()
for i in range(len(table)):
for value in table[i]:
print(value, end="\t\t")
print()
#checks that user selected column exists in the data set
def verify_column(headers, col_name):
column_exists = False
while column_exists == False:
for i in range(len(headers)):
if (headers[i] == col_name):
column_index = i
column_exists = True
break
if (i > len(headers) - 2):
print("Column with that name does not exist. Enter a column with a valid name.\n")
col_name = (input("Please enter the name of the column you would like to compute stats from.\n"))
return column_index
#converts selected column data into a list of floats
def get_column(headers, column_index, data):
user_column = []
user_column = [row[column_index] for row in data]
float_list = [float(i) for i in user_column]
return float_list
#returns the total number of elements in the column
def compute_count(user_column):
number_of_elements_in_list = len(user_column)
string_num_elements = str(number_of_elements_in_list)
print("There are " + string_num_elements + " elements in the column.")
return number_of_elements_in_list
#computes the mean of the column
def compute_mean(user_column):
sum_of_list = sum(user_column)
sum_str = str(sum_of_list)
average_of_list = round(sum_of_list / len(user_column),2)
average_of_list_str = str(average_of_list)
return average_of_list
#computes the standard deviation of the column
def compute_stdev(average, list):
stdev = 0
stdev_calculation = []
tmp = 0
for i in range(len(list)):
tmp = ((list[i] - average) ** 2)
stdev_calculation.append(tmp)
stdev = compute_mean(stdev_calculation)
stdev = round(stdev ** (1/2),2)
stdev_str = str(stdev)
print("The standard deviation of the list is: " + stdev_str)
return stdev
#computes the median of the column
def compute_median(num_elements, list):
global list_median
list.sort()
if (num_elements % 2) == 0:
locate_median = num_elements // 2
list_median = round(((list[locate_median] + list[locate_median - 1]) / 2),2)
else:
locate_median = ((num_elements // 2) + 1)
list_median = list[locate_median - 1]
list_median_str = str(list_median)
print("The median of the list is:", list_median_str)
return list_median
#computes the smallest and largest values of the column
def compute_smallest_and_largest(list):
list.sort()
smallest = min(list)
largest = max(list)
print("The smallest number in the list is:", smallest)
print("The largest number in the list is:", largest)
return smallest, largest
The code below takes data from a csv file (a spreadsheet) and is able to display all the data and calculate some statisitcs like last time, except it is over a much larger data set and it computes more advanced stats like standard deviation. You also select which part of the data you would like to collect stats on.
data = read_data("fitbit_data.csv")
clean_lines(data)
data_table = restructure_data_into_table(data)
headers = data_table.pop(0)
for title in headers:
print(title)
#prints the whole spreadsheet
display_data(headers, data_table)
col_name = (input("Please enter the name of the column you would like to compute stats from.\n"))
column_index = verify_column(headers, col_name)
list_to_calculate = get_column(headers, column_index, data_table)
print()
total_elements = compute_count(list_to_calculate)
avg_of_list = compute_mean(list_to_calculate)
print("The mean of the list is: " + str(avg_of_list))
compute_stdev(avg_of_list, list_to_calculate)
compute_median(total_elements, list_to_calculate)
compute_smallest_and_largest(list_to_calculate)
Steps Calories Burned Max Heartrate Low Heartrate Please enter the name of the column you would like to compute stats from. Steps There are 9 elements in the column. The mean of the list is: 9801.89 The standard deviation of the list is: 4294.1 The median of the list is: 10453.0 The smallest number in the list is: 1597.0 The largest number in the list is: 15862.0
(1597.0, 15862.0)
Graphing¶
from matplotlib import pyplot as plt
# Plot the data
#plt.scatter(iris.data[:, 0], iris.data[:, 1])
dict = {1:2, 1:1, 1:3, 2:1, 5:3, 4:4, 0:2}
for i in dict:
plt.scatter(i, dict[i])
plt.xlabel("Length")
plt.ylabel("Width")
plt.title("Iris Flowers")
plt.show()
Exercise 8: Add a title, x label, and y label to the following graph¶
# plot data from range() and list
plt.scatter(range(1,10), [1, 5, 2, 16, 30, 2, -4. -9, 1, 0])
<matplotlib.collections.PathCollection at 0xffff77cca2f0>
Find the Errors¶
Try to find and fix all of the errors in the following code. You will know if you have done it correctly if you can play hangman with a neighbor!
import time
word = "syzygy"
input("What is your name? ")
print("Hello, " + name, "Time to play hangman!")
print("")
time.sleep(1)
print("Start guessing...)
time.sleep(0.5)
guesses = ''
turns = 10
while turns > 0:
failed = 0
for char in word:
if char in guesses:
print char
else:
print("_")
failed + 1
if failed = 0:
print("You won")
break
print()
guess = input("guess a character:"
guesses += guess
if guess not in word:
turns -= 1
print("Wrong")
print("You have", turns, 'more guesses')
if turns == 0:
print("You Lose")
Input In [31] print("Start guessing...) ^ SyntaxError: unterminated string literal (detected at line 11)